<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>菜单管理<small>菜单项管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单项管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table
                            :data="dataList"
                            style="width: 100%;margin-bottom: 20px;"
                            row-key="id"
                            :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                        <el-table-column
                                prop="name"
                                label="菜单名称"
                                sortable
                                width="180">
                        </el-table-column>
                        <el-table-column
                                prop="linkUrl"
                                label="菜单连接"
                                sortable
                                width="180">
                        </el-table-column>
                        <el-table-column
                                prop="path"
                                label="菜单路径">
                        </el-table-column>
                        <el-table-column
                                prop="priority"
                                label="菜单优先级顺序">
                        </el-table-column>
                        <el-table-column
                                prop="icon"
                                label="菜单图标">
                            <template slot-scope="scope">
                                <i class="fa" :class="scope.row.icon"></i>
                            </template>
                        </el-table-column>
                        <el-table-column
                                prop="description"
                                label="菜单描述">
                        </el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                   <!-- <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                        <el-table-column prop="linkUrl" label="菜单连接" align="center"></el-table-column>
                        <el-table-column prop="path" label="菜单路径" align="center"></el-table-column>
                        <el-table-column prop="priority" label="菜单优先级顺序" aligIn="center"></el-table-column>
                        <el-table-column prop="description" label="菜单描述" align="center"></el-table-column>
                        &lt;!&ndash;<el-table-column prop="parentMenuId" label="父菜单Id" align="center"></el-table-column>&ndash;&gt;
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>-->
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">

                                        <el-form-item label="菜单类型" prop="level">
                                            <el-select v-model="formData.level" @change="levelChange(formData.level)" >
                                                <el-option label="父菜单" v-bind:value="1"></el-option>
                                                <el-option label="子菜单" v-bind:value="2"></el-option>
                                            </el-select>
                                        </el-form-item>
                                        </template>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="12" >
                                        <template v-if="iconFormVisible === 'true'">
                                            <el-form-item label="菜单图标" prop="icon">
                                                <el-select v-model="formData.icon" >
                                                    <el-option  v-for="icon in iconList" v-bind:value="icon">
                                                        <i class="fa" :class="icon"></i>
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </template>
                                    </el-col>
                                    <el-col :span="12" >
                                        <template v-if="parentFormVisible === 'true'">
                                            <el-form-item label="父菜单选项" prop="parentMenuId">
                                                <el-select v-model="formData.parentMenuId" @change="parentChange(formData.parentMenuId)">
                                                    <el-option v-for="c in selectData" v-bind:label="c.name" v-bind:value="c.id"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </template>
                                    </el-col>
                                    <el-col :span="12" style="display: none">
                                        <el-form-item label="菜单路径" >
                                            <el-input v-model="formData.path"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单连接" prop="linkUrl">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                    <!-- <el-col :span="12">
                                         <el-form-item label="菜单路径" prop="path">
                                             <el-input v-model="formData.path"/>
                                         </el-form-item>
                                     </el-col>-->
                                    <el-col :span="12">
                                        <el-form-item label="菜单优先级" prop="priority">
                                            <el-input v-model="formData.priority"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
                                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                                </div>
                        </el-dialog>

                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">

                                            <el-form-item label="菜单类型" prop="level">
                                                <el-select v-model="formData.level" @change="levelChange(formData.level)" >
                                                    <el-option label="父菜单" v-bind:value="1"></el-option>
                                                    <el-option label="子菜单" v-bind:value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </template>
                                    </el-col>
                                </el-row>

                                <el-row>
                                    <el-col :span="12" >
                                        <template v-if="iconFormVisible === 'true'">
                                            <el-form-item label="菜单图标" prop="icon">
                                                <el-select v-model="formData.icon" >
                                                    <el-option  v-for="icon in iconList" v-bind:value="icon">
                                                        <i class="fa" :class="icon"></i>
                                                    </el-option>
                                                </el-select>
                                            </el-form-item>
                                        </template>
                                    </el-col>
                                    <el-col :span="12" >
                                        <template v-if="parentFormVisible === 'true'">
                                            <el-form-item label="父菜单选项" prop="parentMenuId">
                                                <el-select v-model="formData.parentMenuId" @change="parentChange(formData.parentMenuId)">
                                                    <el-option v-for="c in selectData" v-bind:label="c.name" v-bind:value="c.id"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </template>
                                    </el-col>
                                    <el-col :span="12" style="display: none">
                                        <el-form-item label="菜单路径" >
                                            <el-input v-model="formData.path"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单连接" prop="linkUrl">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                    <!-- <el-col :span="12">
                                         <el-form-item label="菜单路径" prop="path">
                                             <el-input v-model="formData.path"/>
                                         </el-form-item>
                                     </el-col>-->
                                    <el-col :span="12">
                                        <el-form-item label="菜单优先级" prop="priority">
                                            <el-input v-model="formData.priority"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <!--<script src="../plugins/elementui/index.js"></script>-->
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:5,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                selectData:[],//父类选项数据
                iconList: ["fa-address-book","fa-certificate","fa-bell","fa-book","fa-coffee","fa-envelop"],//图标列表
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                iconFormVisible:false,//菜单图标选项是否可见
                parentFormVisible:false,//编辑父类选项是否可见
                rules: {//校验规则
                    name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }],
                    level: [{ required: true, message: '菜单类型为必填项', trigger: 'blur' }],
                    priority: [{ required: true, message: '菜单优先级顺序为必填项', trigger: 'blur' }],
                    parentMenuId: [{ required: true, message: '父级菜单为必选项', trigger: 'blur' }],
                    icon: [{ required: true, message: '图标为必选项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                //调用分页查询方法完成分页查询
                this.findPage();
            },
            methods: {
                //编辑
                handleEdit() {
                    //进行表单校验
                    this.$refs['dataEditForm'].validate((valid) => {
                        if(valid){
                            //如果校验通过，发送ajax请求，将表单数据提交到Controller
                            axios.post("/menu/edit.do",this.formData).then((res) => {

                                if (res.data.flag){

                                    this.dialogFormVisible4Edit = false;
                                    this.iconFormVisible = "false";
                                    this.parentFormVisible = "false";
                                    this.$message.success(res.data.message)
                                    this.findPage();

                                } else {
                                    this.$message.error(res.data.message)
                                }

                            }).catch();


                        }else{
                            //校验不通过，提示错误信息
                            this.$message.error("表单数据校验失败，请检查输入是否正确！");
                        }
                    });

                },
                //添加
                handleAdd () {
                    //进行表单校验
                    this.$refs['dataAddForm'].validate((valid) => {
                        if(valid){
                            //如果校验通过，发送ajax请求，将表单数据提交到Controller
                            axios.post("/menu/add.do",this.formData).then((res) => {

                                if (res.data.flag){

                                    this.resetForm();
                                    this.selectData = null;
                                    /*父类菜单选项清空*/
                                    /*隐藏父菜单禁用*/
                                    this.parentFormVisible = "disabled";
                                    /*隐藏新增窗口*/
                                    this.dialogFormVisible = false;
                                    this.$message.success(res.data.message)
                                    this.findPage();

                                } else {
                                    this.$message.error(res.data.message)
                                }

                            }).catch((r) => {

                            });

                        }else{
                            //校验不通过，提示错误信息
                            this.$message.error("表单数据校验失败，请检查输入是否正确！");
                        }
                    });
                },
                //分页查询
                findPage() {
                    var param = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };

                    //发送ajax请求，进行分页查询
                    axios.post("/menu/findPage.do",param).then((res) => {
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                    });

                },
                // 重置表单
                resetForm() {
                    this.formData = {};
                },
                // 弹出添加窗口
                handleCreate() {

                    //将添加窗口展示出来
                    this.dialogFormVisible = true;
                    this.resetForm();
                    this.selectData = null;
                    /*父类菜单选项清空*/
                    this.parentFormVisible = "false";
                    this.iconFormVisible = "false";

                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    this.dialogFormVisible4Edit = true;
                    //回显数据
                    axios.post("/menu/findById.do?id="+row.id).then((res) => {

                        if (res.data.flag){
                            this.formData = res.data.data;

                            /*判断是 父类菜单还是子类菜单*/
                            if (res.data.data.parentMenuId != null){
                                /*子菜单*/
                                /*父类菜单选项展示*/
                                this.parentFormVisible = 'true';
                                this.iconFormVisible = "false";
                                /*设置选项为子菜单*/
                                this.formData.level = 2
                                /*调用方法查询父菜单选项*/
                                this.levelChange(2);
                            }else {
                                /*父菜单*/
                                /*设置选项为父菜单*/
                                this.formData.level = 1
                                this.parentFormVisible = "false";
                                this.iconFormVisible = "true";
                            }

                        } else {
                            this.$message.error(res.data.message)
                        }


                    }).catch((res) => {

                        this.$message.error(res.data.message);

                    })

                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;//指定最新的页码
                    this.findPage();
                },

                showMessage(r){
                    if(r == 'Error: Request failed with status code 403'){
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else{
                        this.$message.error('未知错误');
                        return;
                    }
                },

                // 删除
                handleDelete(row) {
                    //弹出确认框
                    this.$confirm('你确定要删除当前数据吗？','提示',{
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.post("/menu/delete.do?id="+row.id).then((res) =>{

                            if (res.data.flag){

                                this.$message.success(res.data.message);
                                this.findPage();

                            } else {
                                this.$message.error(res.data.message);
                            }

                        }).catch((r) => {

                        });
                    }).catch(() => {
                        this.$message("已取消");
                    });
                },
                /*菜单类型选项*/
                levelChange(value){

                    /*选择父类型 , 清空父类选项 数据*/
                    if(value == 1){

                        this.iconFormVisible = 'true';
                        this.parentFormVisible = 'false';
                        /*防止子菜单修改为父菜单*/
                        this.formData.parentMenuId = null;
                        this.selectData = null;

                    }else if (value == 2){/*选择子类型 回显父类选项 数据*/

                        axios.post("/menu/findAllParentMenu.do").then((res) => {

                            if (res.data.flag) {
                                this.selectData = res.data.data
                            }

                        }).catch((r) => {

                        });
                        console.log(this.parentFormVisible,this.iconFormVisible);
                        this.iconFormVisible = 'false';
                        this.parentFormVisible = 'true';

                    }

                },
                /*菜单路径设置*/
                parentChange(value){

                    axios.post("/menu/findPathById.do?id="+value).then((res) => {

                        if (res.data.flag) {
                            this.formData.path = res.data.data;
                        }

                    }).catch((r) => {

                    });

                }
            }
        })
    </script>
</html>